package com.dimples.runner;

import com.dimples.common.constant.StrPool;
import com.dimples.common.enums.ViewEnum;
import com.dimples.controller.view.BaseWebController;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;

import java.net.InetAddress;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2020/1/21
 */
@Slf4j
@Component
public class DimplesStartedUpRunner implements ApplicationRunner {

    public static final String WINDOWS = "windows";

    private ConfigurableApplicationContext context;

    public DimplesStartedUpRunner(ConfigurableApplicationContext context) {
        this.context = context;
    }

    @Value("${server.port:8080}")
    private String port;
    @Value("${server.servlet.context-path:}")
    private String contextPath;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (context.isActive()) {
            InetAddress address = InetAddress.getLocalHost();
            String urlBase = String.format("http://%s:%s", address.getHostAddress(), port);
            String url = urlBase;
            contextPath = StrUtil.isBlank(contextPath) ? StrPool.SLASH : contextPath;
            url += contextPath;

            log.info("   ,--.,--.                 ,--.                                      ,--.    ");
            log.info(" ,-|  |`--',--,--,--. ,---. |  | ,---.  ,---.,-----.,--.   ,--. ,---. |  |-.  ");
            log.info("' .-. |,--.|        || .-. ||  || .-. :(  .-''-----'|  |.'.|  || .-. :| .-. ' ");
            log.info("\\ `-' ||  ||  |  |  || '-' '|  |\\   --..-'  `)      |   .'.   |\\   --.| `-' | ");
            log.info(" `---' `--'`--`--`--'|  |-' `--' `----'`----'       '--'   '--' `----' `---'  ");
            log.info("                     `--'                                                     ");
            log.info("DIMPLES 权限系统启动完毕，地址：{}", url + BaseWebController.WEB_PRE + ViewEnum.LOGIN.getValue());
            log.info("DIMPLES 文档地址：{}", urlBase + "/doc.html");

            String os = System.getProperty("os.name");
            // 默认为 windows时才自动打开页面
            if (WINDOWS.equalsIgnoreCase(os)) {
                //使用默认浏览器打开系统登录页
                Runtime.getRuntime().exec("cmd  /c  start " + url);
            }
        }
    }
}

















